package net.sapfree.core.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sapfree.core.code.JsonEncodeAdapter;
import net.sapfree.core.code.ReplaceRule;

public class RecordJsonEncoder implements JsonEncodeAdapter
{
	private Map<String,List<ReplaceRule>> m_map = new HashMap<String,List<ReplaceRule>> ();
	public void addReplaceRule(String columnName, ReplaceRule rule)
	{
		List<ReplaceRule> ruleList = m_map.get(columnName);
		if(ruleList == null)
		{
			ruleList = new ArrayList<ReplaceRule>();
			m_map.put(columnName, ruleList);
		}		
		ruleList.add(rule);
	}
	
	public String encode(String columnName, String value)
	{
		List<ReplaceRule> ruleList = m_map.get(columnName);
		if(ruleList == null) return value;
		Iterator<ReplaceRule> it = ruleList.iterator();
		while(it.hasNext())
		{
			ReplaceRule rule = it.next();
			value = rule.doReplace(value);
		}
		return value;
	}

}
